package com.exwave.socialgame.model_society.service;

import com.exwave.socialgame.model_user.dao.UserDao;
import com.exwave.socialgame.model_user.entity.User;
import com.exwave.socialgame.model_society.dao.CityteamsDao;
import com.exwave.socialgame.model_society.dao.ResponseDao;
import com.exwave.socialgame.model_society.entity.Cityteams;
import com.exwave.socialgame.model_society.entity.Response;
import com.exwave.socialgame.myutils.WolfGameUtils;
import com.exwave.socialgame.myutils.ParamsException;

import org.springframework.stereotype.Service;

import java.util.List;

import javax.annotation.Resource;

/**
 * (Cityteams)表服务实现类
 *
 * @author makejava
 * @since 2021-12-13 11:30:30
 */
@Service("cityteamsService")
public class CityteamsServiceImpl implements CityteamsService {
    @Resource
    private CityteamsDao cityteamsDao;
    @Resource
    private ResponseDao responseDao;
    @Resource
    private UserDao userDao;

    /**
     * 通过ID查询单条数据
     *
     * @param comid 主键
     * @return 实例对象
     */
    @Override
    public Cityteams queryById(Integer comid) {
        Cityteams cityteams = this.cityteamsDao.queryById(comid);
        WolfGameUtils.isNullOrEmpty(cityteams, "帖子不存在");
        return cityteams;
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<Cityteams> queryAllByLimit(int offset, int limit) {
        if (offset < 0 || limit < 0)
            throw new ParamsException();
        List<Cityteams> list = this.cityteamsDao.queryAllByLimit(offset, limit);
        return list;
    }


    @Override
    public List<Cityteams> queryAllByUID(int userid) {
        User user = this.userDao.queryById(userid);
        WolfGameUtils.isNullOrEmpty(user, "该用户不存在");
        List<Cityteams> cityteamsList = this.cityteamsDao.queryAllByUID(userid);
        WolfGameUtils.isListNull(cityteamsList, "该用户太懒了，什么都没发");
        return cityteamsList;
    }

    /**
     * 新增数据
     *
     * @param cityteams 实例对象
     * @return 实例对象
     */
    @Override
    public Cityteams insert(Cityteams cityteams) {
        WolfGameUtils.isNullOrEmpty(cityteams, "您发布了空的内容");
        //判断用户是否存在
        User user = this.userDao.queryById(cityteams.getUserid());
        WolfGameUtils.isNullOrEmpty(user, "用户不存在");
        cityteams.setCnum(1); //初始响应人数为1
        this.cityteamsDao.insert(cityteams);
        /*将房主加入响应表里面*/
        Response response = new Response();
        response.setUid(cityteams.getUserid());
        response.setCid(cityteams.getComid());
        this.responseDao.insert(response);
        return this.cityteamsDao.queryById(cityteams.getComid());
    }

    /**
     * 修改数据
     *
     * @param cityteams 实例对象
     * @return 实例对象
     */
    @Override
    public Cityteams update(Cityteams cityteams) {
        WolfGameUtils.isNotNullOrEmpty(cityteams.getComid(), "更新失败，请检查ComID");
        WolfGameUtils.isIllegal(cityteams.getComid());
        WolfGameUtils.isNullOrEmpty(this.cityteamsDao.queryById(cityteams.getComid()), "帖子不存在，请检查ComID");
        this.cityteamsDao.update(cityteams);
        return this.queryById(cityteams.getComid());
    }

    /**
     * 通过主键删除数据
     *
     * @param comid 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer comid) {
        Cityteams cityteams = this.cityteamsDao.queryById(comid);
        WolfGameUtils.isNullOrEmpty(cityteams, "帖子不存在，请检查ComID");
        return this.cityteamsDao.deleteById(comid) > 0;
    }
}